package com.edu.smsys.service.impl;

import com.edu.smsys.dao.entity.CensusEntity;
import com.edu.smsys.dao.entity.StudentEntity;
import com.edu.smsys.dao.mapper.CensusEntityMapper;
import com.edu.smsys.model.form.CensusAddForm;
import com.edu.smsys.model.form.CensusQueryForm;
import com.edu.smsys.model.form.CensusUpdateForm;
import com.edu.smsys.model.vo.CensusVO;
import com.edu.smsys.service.BaseService;
import com.edu.smsys.util.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学籍管理逻辑类
 * 2019年12月16日
 */
@Service
public class CensusService implements BaseService<CensusEntity> {
    @Autowired
    private CensusEntityMapper censusEntityMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    /**
     * 查询所有学籍
     * @return
     */
    @Override
    public List<CensusEntity> findAll() {
        return censusEntityMapper.queryCensuses();
    }

    /**
     * 获得学籍的视图对象列表
     * @return
     */
    public List<CensusVO> findCensusAll(){
        List<CensusVO> voList=new ArrayList<>();
        List<CensusEntity> entityList=findAll();
        if (entityList!=null&&entityList.size()>0){
            for (CensusEntity entity:entityList){
                CensusVO vo=new CensusVO();
                BeanUtils.copyProperties(entity,vo,"studentEntity","teacher");
                voForStudentHandle(vo);
                voForTeacherHandle(vo,entity);
                voList.add(vo);
            }
        }
        return voList;
    }


    /**
     * 向视图对象中添加学生信息
     * @param vo
     */
    private void voForStudentHandle(CensusVO vo) {
        List<StudentEntity> entityList=studentService.queryAllStudent();
        for (StudentEntity entity:entityList){
            int id=vo.getId();
            if (entity.getCensusId()==id){
                vo.setStudentEntity(entity);
            }
        }
    }

    /**
     * 向视图对象中添加复制录入改信息的老师信息
     * @param vo
     * @param entity
     */
    private void voForTeacherHandle(CensusVO vo, CensusEntity entity) {
        vo.setTeacher(teacherService.findEntityById(entity.getTeacherId()));
    }

    /**
     * 查询学籍根据学籍id
     * @param id
     * @return
     */
    @Override
    public CensusEntity findEntityById(int id) {
        return censusEntityMapper.queryCensusById(id);
    }

    /**
     * 查询学籍根据学籍实体
     * @param entity
     * @return
     */
    @Override
    public List<CensusEntity> findEntity(CensusEntity entity) {
        return censusEntityMapper.queryCensusByCensus(entity);
    }

    /**
     * 修改学籍信息
     * @param entity
     * @return
     */
    @Override
    public int updateEntity(CensusEntity entity) {
        return censusEntityMapper.updateCensusById(entity);
    }

    /**
     * 修改学籍信息
     * @param form
     */
    public void updateEntity(CensusUpdateForm form){
        CensusEntity entity=new CensusEntity();
        BeanUtils.copyProperties(form,entity);
        StudentEntity studentEntity=studentService.findEntityById(form.getStudentId());
        studentEntity.setCensusId(form.getId());
        studentService.updateEntity(studentEntity);
        entity.setGraduationTime(Util.str2Date(form.getGraduationTime(),"yyyy-MM-dd"));
        updateEntity(entity);
    }
    /**
     * 插入学籍信息
     * @param entity
     * @return
     */
    @Override
    public int addEntity(CensusEntity entity) {
        return censusEntityMapper.insertCensus(entity);
    }

    /**
     * 插入学籍信息
     * @param form
     * @return
     */
    public int addEntity(CensusAddForm form) {
        CensusEntity entity=new CensusEntity();
        BeanUtils.copyProperties(form,entity);
        entity.setGraduationTime(Util.str2Date(form.getGraduationTime(),"yyyy-MM-dd"));
        int change=censusEntityMapper.insertCensus(entity);
        StudentEntity studentEntity=studentService.findEntityById(form.getStudentId());
        studentEntity.setCensusId(entity.getId());
        studentService.updateEntity(studentEntity);
        return change;
    }

    /**
     * 删除班级根据id
     * @param id
     * @return
     */
    @Override
    public int deleteEntity(int id) {
        return censusEntityMapper.deleteCensusById(id);
    }

    /**
     * 根据条件查询学籍信息
     * @param form
     * @return
     */
    public List<CensusVO> findCensusVOByform(CensusQueryForm form) {
        List<CensusVO> voList=new ArrayList<>();
        CensusEntity query=new CensusEntity();
        String currentEnrol=form.getCurrentEnrol();
        String graduationTimeStr=form.getGraduationTime();
        if (!currentEnrol.isEmpty()){
            query.setCurrentEnrol(currentEnrol);
        }
        if (!graduationTimeStr.isEmpty()&&!graduationTimeStr.equals("")){
            Date date= Util.str2Date(form.getGraduationTime(),"yyyy-MM-dd");
            query.setGraduationTime(date);
        }
        List<CensusEntity> censusEntityList=findEntity(query);
        if (censusEntityList!=null&&censusEntityList.size()>0){
            for (CensusEntity entity:censusEntityList){
                CensusVO vo=new CensusVO();
                BeanUtils.copyProperties(entity,vo,"studentEntity","teacher");
                voForStudentHandle(vo);
                voForTeacherHandle(vo,entity);
                voList.add(vo);
            }
        }else{
            if (!currentEnrol.isEmpty()||!graduationTimeStr.isEmpty()&&!graduationTimeStr.equals("")){
                voList=null;
            }else{
                voList=findCensusAll();
            }
        }
        return voList;
    }
}
